package pers.qianyu.month_202012.date_20201211;

import org.junit.*;

import java.util.*;

/**
 * 39. 组合总和
 * https://leetcode-cn.com/problems/combination-sum/
 *
 * @author mizzle rain
 * @date 2020-12-11 16:28
 */
public class CombinationSum {
    List<List<Integer>> res = new ArrayList<>();

    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        ArrayList<Integer> path = new ArrayList<>();
        backTracking(path, candidates, target, 0);
        return res;
    }

    private void backTracking(ArrayList<Integer> path, int[] candidates, int target, int startIndex) {
        if (0 == target) {
            res.add(new ArrayList<>(path));
            return;
        } else if (target < 0) {
            return;
        }
        for (int i = startIndex; i < candidates.length; i++) {
            path.add(candidates[i]);
            backTracking(path, candidates, target - candidates[i], i);
            path.remove(path.size() - 1);
        }
    }

    @Test
    public void test1() {
        int[] candidates = {2, 3, 6, 7};
        int target = 7;
        List<List<Integer>> result = new CombinationSum().combinationSum(candidates, target);
        result.forEach(System.out::println);
    }
}
